rem Heo Run Wizard

Option Explicit

Dim oIE      ' Internet Explorer object
Dim oBut
Dim oFso
Dim oSpan
Dim oPwd, oHost, oPath1, oPath2
Dim quit

Const ForWriting = 2

Dim problem_name
Dim instance_name
Dim cpu
Dim randseed
Dim method_check(3) 'ga, sa, ga_sa
Dim method_name(3)
method_name(1) = "ga"
method_name(2) = "sa"
method_name(3) = "ga_sa"

Dim arch_check(2) 'x86, x64
Dim arch_name(2)
arch_name(1) = ""
arch_name(2) = "x64"

Dim tech_check(3) 'omp, mpi1, mpi2
Dim tech_name(3)
tech_name(1) = "omp"
tech_name(2) = "mpi"
tech_name(3) = "mpi"
Dim mpi1_path, mpi2_path, mpi_pwd, mpi_host
Dim mpi1_path_check, mpi2_path_check, mpi_pwd_check, mpi_host_check
Dim mpi_localonly, mpi_shm

' Launch Internet Explorer, and connect event handler.
Set oIE  = WScript.CreateObject("InternetExplorer.Application", "IE_")

Set oFso = WScript.CreateObject("Scripting.FileSystemObject")

oIE.Left = 50           ' Window position and other properties
oIE.Top = 100 
oIE.Width = 600
oIE.Height = 800
oIE.MenuBar = 0         ' No menu
oIE.ToolBar = 0
oIE.StatusBar = 0
' Commented out because it causes a corrupted window border. 
' oIE.Resizable = 0     ' Disable resizing.
oIE.navigate GetPath() & "wizard_run.htm"  ' Load form.

quit = false
Set oBut = oIE.document.getElementById("generate")
Set oBut.onclick = GetRef("GetFormValue")
oBut.value = "Generate"

Set oSpan  = oIE.document.getElementById("status")

Set oPwd   = oIE.document.getElementById("mpi_pwd")
Set oPwd.onchange = GetRef("PwdChange")
Set oHost  = oIE.document.getElementById("mpi_host")
Set oHost.onchange = GetRef("HostChange")
Set oPath1 = oIE.document.getElementById("mpi1_path")
Set oPath1.onchange = GetRef("Path1Change")
Set oPath2 = oIE.document.getElementById("mpi2_path")
Set oPath2.onchange = GetRef("Path2Change")


oIE.Visible = 1         ' Keep visible.

' Important: wait until Internet Explorer is ready.
Do While (oIE.Busy)
    WScript.Sleep 200   ' Suspend for 200 milliseconds.  
Loop

' Wait until the user clicks the OK button.
Do                     ' Wait until the OK button is clicked.
    WScript.Sleep 200  ' Suspend for 0.2 second.
Loop While (Not quit)

Set oIE = Nothing    ' Reset object variable

' Event handler and helper

Sub IE_OnQuit()
    ' Event handler called when IE terminates.
    quit = true 'Indicate form is closed
End Sub

Function WriteFile(fname, ByRef text)
    Dim stream
    Set stream = oFso.OpenTextFile(fname, ForWriting, True, 0) 'Opens the file as ASCII
    stream.Write text
End Function

Function WriteCmdFile(run_dir, t_num, a_num, s_num, ByRef name_list)
    Dim cmd_name, cmd_text, platform, method, technology
    platform = arch_name(a_num)
    method = method_name(s_num)
    technology = tech_name(t_num)
    cmd_name = run_dir & "\" & method & "_" & technology
    If t_num = 3 Then 'MPI 2.x
        cmd_name = cmd_name & "2"
    End If
    If platform <> "" Then
        cmd_name = cmd_name & "_" & platform
    End If
    cmd_name = cmd_name & ".cmd"

    Dim full_project, exe_path, res_dir
    full_project = problem_name & "_" & method & "_" & technology
    exe_path = "..\" & method & "\" & technology & "\Release\"
    If platform <> "" Then
       exe_path = exe_path & platform & "\"
    End If
    exe_path = exe_path & full_project & ".exe"
    res_dir = "..\res\" & instance_name & "\%1"

    cmd_text = "@echo off" & vbCrLf & _
        "if not exist """ & exe_path & """ (echo ERROR: " & exe_path & " not found" & vbCrLf & _
        "echo Please build project " & full_project & vbCrLf & _
        "goto :eof" & vbCrLf & _
        ")" & vbCrLf & _
        vbCrLf & _
        "for %%R in (" & randseed & ") do for %%G in (" & cpu & ") do (call :run %%R %%G)" & vbCrLf & _
        "goto :eof" & vbCrLf & _
        vbCrLf & _
        ":run" & vbCrLf & _
        "echo randseed: %1, cpu: %2" & vbCrLf & _
        "if not exist """ & res_dir & """ @mkdir """ & res_dir & """" & vbCrLf

    If t_num = 1 Then     'OpenMP
       cmd_text = cmd_text & _
            "set OMP_NUM_THREADS=%2" & vbCrLf
    Else
       If t_num = 2 Then  'MPI 1.x
           cmd_text = cmd_text & """" & mpi1_path & """ "
       Else               'MPI 2.x
           cmd_text = cmd_text & """" & mpi2_path & """ "
       End If
       If mpi_localonly Then cmd_text = cmd_text & "-localonly "
       If mpi_shm Then       cmd_text = cmd_text & "-channel shm "
       If mpi_pwd <> "" Then cmd_text = cmd_text & "-pwdfile """ & mpi_pwd & """ "
       If mpi_host <> "" Then cmd_text = cmd_text & "-machinefile """ & mpi_host & """ "
       cmd_text = cmd_text & "-np %2 "
    End If
    cmd_text = cmd_text & _
        """" & exe_path & """ %1 " & _
        """" & method & "_" & instance_name & ".xml"" > " & _
        """" & res_dir & "\%2." & method & "." & technology & ".txt"""

    WriteFile cmd_name, cmd_text
    name_list = name_list & " " & cmd_name
End Function

'Little usability improvements
Sub PwdChange
    If oIE <> vbNull Then oIE.Document.RunForm.mpi_pwd_check.checked = true
End Sub

Sub HostChange
    If oIE <> vbNull Then oIE.Document.RunForm.mpi_host_check.checked = true
End Sub

Sub Path1Change
    If oIE <> vbNull Then oIE.Document.RunForm.mpi1_path_check.checked = true
End Sub

Sub Path2Change
    If oIE <> vbNull Then oIE.Document.RunForm.mpi2_path_check.checked = true
End Sub

Sub GetFormValue()
    ' This happens if the user clicks the Generate button.
    oSpan.innerHTML = ""

    problem_name     = oIE.Document.RunForm.problem_name.value
    tech_check(1)    = oIE.Document.RunForm.omp.checked
    tech_check(2)    = oIE.Document.RunForm.mpi1.checked
    tech_check(3)    = oIE.Document.RunForm.mpi2.checked
    mpi1_path        = oIE.Document.RunForm.mpi1_path.value
    mpi2_path        = oIE.Document.RunForm.mpi2_path.value
    mpi1_path_check  = oIE.Document.RunForm.mpi1_path_check.checked
    mpi2_path_check  = oIE.Document.RunForm.mpi2_path_check.checked
    mpi_pwd          = oIE.Document.RunForm.mpi_pwd.value
    mpi_host         = oIE.Document.RunForm.mpi_host.value
    mpi_pwd_check    = oIE.Document.RunForm.mpi_pwd_check.checked
    mpi_host_check   = oIE.Document.RunForm.mpi_host_check.checked
    mpi_localonly    = oIE.Document.RunForm.mpi_localonly.checked
    mpi_shm          = oIE.Document.RunForm.mpi_shm.checked
    arch_check(1)    = oIE.Document.RunForm.x86.checked
    arch_check(2)    = oIE.Document.RunForm.x64.checked
    cpu              = oIE.Document.RunForm.cpu.value
    randseed         = oIE.Document.RunForm.randseed.value
    method_check(1)  = oIE.Document.RunForm.ga.checked
    method_check(2)  = oIE.Document.RunForm.sa.checked
    method_check(3)  = oIE.Document.RunForm.gasa.checked
    instance_name    = oIE.Document.RunForm.instance_name.value

    If Not mpi1_path_check Then mpi1_path = "C:\Program Files\MPICH\mpd\bin\mpirun.exe"
    If Not mpi2_path_check Then mpi2_path = "C:\Program Files\MPICH2\bin\mpiexec.exe"
    If Not mpi_pwd_check   Then mpi_pwd   = "..\..\..\logins\logins.txt"
    If Not mpi_host_check  Then mpi_host  = "..\..\..\logins\hosts.txt"

    Dim i, j, k, run_dir, name_list
    name_list = ""
    run_dir = GetPath() & problem_name & "\run"
    If Not oFso.FolderExists(run_dir) Then oFso.CreateFolder(run_dir)

    For i = 1 to 3
        If tech_check(i) Then
	    For j = 1 to 2
                If arch_check(j) Then
                    For k = 1 to 3
                        If method_check(k) Then
                            WriteCmdFile run_dir, i, j, k, name_list
                        End If
                    Next
                End If
            Next
        End If
    Next

    If name_list <> "" Then oSpan.innerHTML = "Command file(s) created successfully:" & name_list
'close form
    'Wscript.Echo "Command file(s) created successfully."
    'quit = True          ' Indicate form is closed.
    'oIE.Quit             ' Close Internet Explorer.
End Sub

Function GetPath()
    ' Get script path because the form (HTML file)
    ' must be in the same folder.
    Dim path
    path = WScript.ScriptFullName
    GetPath = Left(path, InStrRev(path, "\"))
End Function



rem eof
